/**
 * Created by Administrator on 2017/5/10.
 */

var roomManager = require('../../../logic/game/roomManager');

module.exports = function(app) {
    return new RoomRemote(app);
};

var RoomRemote = function(app) {
    this.app = app;
};

var remote = RoomRemote.prototype;

remote.sync = function(msg, callback) {
    callback && callback();

    var roomId = +msg.rid;

    var room = roomManager.getRoom(roomId);
    if (!room || room.isDestroyed()) {
        return;
    }

    var uid = +msg.uid;
    var player = room.getPlayerByUid(uid);
    if (!player) {
        return;
    }

    player.updateUser(msg.user);
}

remote.offline = function(uid, msg, callback) {
    callback();

    var roomId = +msg.rid;

    var room = roomManager.getRoom(roomId);
    if (!room || room.isDestroyed()) {
        return;
    }

    var player = room.getPlayerByUid(uid);
    if (!player) {
        return;
    }

    room.handlePlayerOffline(player);
}